package com.ax.ms.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ax.ms.service.PrintService;
import com.ax.ms.service.domain.Printer;

/**
 * 打印 Controller
 * 
 * @author xgl
 * @date 2017年5月10日
 * 
 */
@RestController
@RequestMapping(value = "/print")
public class PrintController {
	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private DiscoveryClient client;

	@Autowired
	private PrintService printService;

	@GetMapping(value = "/{code}")
	public Printer getOne(@PathVariable("code") long code) {
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("/print:get, host:" + instance.getHost() + ", port:" + instance.getPort() + ", service_id:" + instance.getServiceId());

		return printService.find(code);
	}

	@PostMapping
	public Printer create(@RequestParam("code") long code, @RequestParam("name") String name) {
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("/print:post, host:" + instance.getHost() + ", port:" + instance.getPort() + ", service_id:" + instance.getServiceId());

		return printService.create(code, name);
	}

	@PutMapping
	public Printer update(@RequestParam("code") long code, @RequestParam("name") String name) {
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("/print:put, host:" + instance.getHost() + ", port:" + instance.getPort() + ", service_id:" + instance.getServiceId());

		return printService.update(code, name);
	}

	@DeleteMapping(value = "/{code}")
	public Printer remove(@PathVariable("code") long code) {
		ServiceInstance instance = client.getLocalServiceInstance();
		logger.info("/print:delete, host:" + instance.getHost() + ", port:" + instance.getPort() + ", service_id:" + instance.getServiceId());

		return printService.remove(code);
	}

}
